package com.QiLin.service.impl;

import com.QiLin.dao.LetterDao;
import com.QiLin.domain.User;
import com.QiLin.domain.letters;
import com.QiLin.service.LetterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LetterImpl implements LetterService {

    @Resource
    LetterDao letterDao;

    public boolean SendService(letters letters, HttpSession httpSession)throws Exception {
        letters.setFromUser((String) httpSession.getAttribute("user"));
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");//设置日期格式
        letters.setTime(df.format(new Date()));
        letters.setCollect(0);
        BufferedWriter wra=new BufferedWriter(new FileWriter("C:/Users/冷咖啡/IdeaProjects/lin/src/main/webapp/datas/"+letters.getFromUser()+letters.getHeadline()+".txt",true));
        wra.write(letters.getContent());
        wra.close();
        letters.setContent(letters.getFromUser()+letters.getHeadline());
        letters.setFromName((String) httpSession.getAttribute("name"));
        int i=letterDao.SendDao(letters);

        if(i==1){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean SendDetection(String ReceiveUser, String Headline) {
        System.out.println(Headline);
        letters lesHe= letterDao.SendDaoHeadline(Headline);
        User lesRe= letterDao.SendDaoReceiveUser(ReceiveUser);
        if(lesHe==null && lesRe!=null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<letters> UpdatesService(HttpSession session){
      List<letters> let=  letterDao.UpdatesDao((String) session.getAttribute("user"));
      List<String> stringList=new ArrayList<String>();
        for(letters le:let){
          stringList.add(le.getContent());
      }
      session.setAttribute("liststring",stringList);
        return let;
    }
}